package com.example.experiment4.web.client;

import com.example.experiment4.model.domain.Article;
import com.example.experiment4.model.domain.Comment;
import com.example.experiment4.service.IArticleService;
import com.example.experiment4.service.ICommentService;
import com.example.experiment4.service.ISiteService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Api(tags = "Index Controller", description = "博客首页及文章相关操作接口")
public class IndexController {
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private IArticleService articleServiceImpl;
    @Autowired
    private ICommentService commentServiceImpl;
    @Autowired
    private ISiteService siteServiceImpl;

    // 博客首页，会自动调转到文章页
    @GetMapping(value = "/")
    @ApiOperation("访问博客首页，默认跳转到文章页")
    private String index(HttpServletRequest request) {
        return this.index(request, 1, 5);
    }

    @GetMapping(value = "/page/{p}")
    @ApiOperation("分页获取文章信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "p", value = "页码，取值应为正整数，代表需要获取第几页的文章数据，用于分页展示文章列表，如输入1则获取第一页文章。", required = true, dataType = "int"),
            @ApiImplicitParam(name = "count", value = "每页文章数量，取值应为正整数，默认值是5，表示每页要展示的文章条数，可根据实际需求传入不同数值来控制每页展示数量。", defaultValue = "5", dataType = "int")
    })
    public String index(HttpServletRequest request, @PathVariable("p") int page,
                        @RequestParam(value = "count", defaultValue = "5") int count) {
        PageInfo<Article> articles = articleServiceImpl.selectArticleWithPage(page, count);
        // 获取文章热度统计信息
        List<Article> articleList = articleServiceImpl.getHeatArticles();
        request.setAttribute("articles", articles);
        request.setAttribute("articleList", articleList);
        logger.info("分页获取文章信息：页码 " + page + "，条数 " + count);
        return "client/index";
    }

    // 文章详情查询
    @GetMapping(value = "/article/{id}")
    @ApiOperation("根据文章ID查询文章详情")
    @ApiImplicitParam(name = "id", value = "文章的唯一标识符，为正整数类型，通过传入对应文章的这个唯一ID，来查询该文章的详细信息。", required = true, dataType = "int")
    public String getArticleById(@PathVariable("id") Integer id, HttpServletRequest request) {
        Article article = articleServiceImpl.selectArticleWithId(id);
        if (article != null) {
            // 查询封装评论相关数据
            getArticleComments(request, article);
            // 更新文章点击量
            siteServiceImpl.updateStatistics(article);
            request.setAttribute("article", article);
            return "client/articleDetails";
        } else {
            logger.warn("查询文章详情结果为空，查询文章id：" + id);
            // 未找到对应文章页面，跳转到提示页
            return "comm/error_404";
        }
    }

    private void getArticleComments(HttpServletRequest request, Article article) {
        if (article.getAllowComment()) {
            // cp表示评论页码，commentPage
            String cp = request.getParameter("cp");
            cp = StringUtils.isBlank(cp) ? "1" : cp;
            request.setAttribute("cp", cp);
            PageInfo<Comment> comments = commentServiceImpl.getComments(article.getId(), Integer.parseInt(cp), 3);
            request.setAttribute("comments", comments);
        }
    }
}